var StatusHtml = function(){
	return {
		success:function(id,url,name,code){
			
			var html ="<li class='success detail' data-id='"+id+"'>"
				+"<div class='left-side'>"+"<img src='"+url+"'/></div>"
				+"<div class='right-side'>"+"<p>"+name+"</p><p>"+code+"</p></div></li>";
			return html;
		},
		danger:function(id,url,name,code){
			var html ="<li class='danger detail' data-id='"+id+"'>"
				+"<div class='left-side'>"+"<img src='"+url+"'/></div>"
				+"<div class='right-side'>"+"<p>"+name+"</p><p>"+code+"</p></div></li>";
			return html;
		},
		warning:function(id,url,name,code){
			var html ="<li class='warning detail' data-id='"+id+"'>"
				+"<div class='left-side'>"+"<img src='"+url+"'/></div>"
				+"<div class='right-side'>"+"<p>"+name+"</p><p>"+code+"</p></div></li>";
			return html;
		}
	};     
}();

var EquipStatus =function(){
	return {
		init:function(){
			SystemCommon.getQueryOperation();
			SystemCommon.getQueryEquipCode();
//			SystemCommon.getQueryEquip();
			EquipStatus.search();
			EquipStatus.changeEquipment();
			$(".time-set .search").trigger('click');
			EquipStatus.toDetail();
			EquipStatus.list();
		},
		//初始化设备
		listEquip : function(index,size){
			$('#pagination').bootstrapPaginator({
			    currentPage: index<=0?1:index,//当前的请求页面。
			    totalPages: size<=0?0:size,//一共多少页。
			    size:"large",//应该是页眉的大小。
			    bootstrapMajorVersion: 2,//bootstrap的版本要求。
			    //shouldShowPage : true,//显示所有的分页操作按钮
			    alignment:"right",
			    numberOfPages:5,//一次显示多少页。
			    itemTexts: function (type, page, current) {//如下的代码是将页眉显示的中文显示我们自定义的中文。
			        switch (type) {
			        case "first": return "首页";
			        case "prev": return "上一页";
			        case "next": return "下一页";
			        case "last": return "末页";
			        case "page": return page;
			        }
			    },

			    //页面变更事件
			    onPageClicked:function (event, originalEvent, type,page) {
			    	EquipStatus.list(page);
			    }
			  
			});
    		if(index==1){
    			$("#pagination").css("display","none");
    		}
		},
		//修改设备编号--带出对应的设备名称
		changeEquipment : function(){
			$(".queryEquipment").on("select2:select",function(){ 
				$.ajax({
					url: ctx +'/equipment/register/getEquipmentByEquipId/get.mvc',
				    type:"post", 
				    dataType:'json',
				    data: {
				    	equipId : $(".queryEquipment").val()
				    },
				    success : function(data){
				    	$(".name").val(data.equipName);
				    },
				    error: function(XMLHttpRequest, textStatus, errorThrown){
				    	alert("系统错误");
				    }
				});
	        }); 
		},
		//获取设备列表
		list : function(currentPage){
			var eqId = $(".time-set .eqId").val();
			var name = $(".time-set .name").val();
			var opId = $(".time-set .operation").val();
			$.ajax({
				url: ctx +'/equipment/status/listEquipStatus/get.mvc',
			    type:"post", 
			    dataType:'json',
			    data : {
			    	eqId : eqId,
			    	equipName : name,
			    	opId:opId,
			    	num : currentPage
		        },
		        success : function(response){
		        	if(response.result == true){
		        		var pageNum =response.data.pageNum;
		        		var pages =1;
		        		var list =response.data.list;
		        		
		        		var html="";
		        		
		        		for(var i =0;i<list.length;i++){
		        			
		        			var image ="";
		        			if(list[i].image == null){
		        				image=ctx+"/image/icon-3234.jpg";
		        			}else{
		        				image="/"+list[i].image;
		        			}
		        			
		        			if(list[i].status == 0){
		        				html+=StatusHtml.success(list[i].id,image,list[i].name,list[i].code);
		        			}else if(list[i].status == 1){
		        				html+=StatusHtml.danger(list[i].id,image,list[i].name,list[i].code);
		        			}else{
		        				html+=StatusHtml.warning(list[i].id,image,list[i].name,list[i].code);
		        			}
		        		}
		        		
		        		$(".device-list .equipmentlist").html(html);
		        		$('.page-num').html("");
		        		EquipStatus.listEquip(pageNum,pages);
		        		
		        	}else{
		        		DialogUtil.warn("系统错误");
		        	}
		        },
			    error: function(XMLHttpRequest, textStatus, errorThrown){
			    	
			    	DialogUtil.warn("系统错误");
			    }
			});
		},
		//查询
		search : function(){
			$(".time-set .search").on("click",function(){
				EquipStatus.list(1);
			});
			
			$(".time-set .name").keypress(function (e) {
	            if (e.which == 13) {
	                $(".time-set .search").trigger("click");
	            }
	        });
			
			//重置
			$(".time-set .reset").on("click",function(){
				$(".name").val("");
				$(".queryEquipment").empty(); 
				$(".operationSearch").empty(); 
			});
			
		},

		
		//跳转设备状态数据
		toDetail : function(){
			$(".equipmentlist").on("click",".detail",function(){
				var equipId =$(this).data("id");
				window.location.href=ctx+"/equipment/status/data/index?id="+equipId;
			});
		}
		
		
		
	};
}();


jQuery(document).ready(function() {
	EquipStatus.init();
	
});

